package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectattach.ProjectAttachDTO;
import com.guldan.jaina.dto.projectextensionrelation.ProjectExtensionRelationDTO;
import com.guldan.jaina.utils.ProjectAttachDomainConvertUtils;
import com.guldan.mapper.db.entity.ProjectAttach;
import com.guldan.mapper.db.entity.ProjectAttachExample;
import com.guldan.mapper.db.entity.ProjectExtensionRelation;
import com.guldan.mapper.db.mapper.ProjectAttachMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectAttachManager extends BaseManager {

    @Autowired
    private ProjectAttachMapperExt projectAttachMapperExt;

    //获取单个项目附件信息
    public ProjectAttachDTO get(Integer projectAttachId) {
    	ProjectAttachDTO dto = ProjectAttachDomainConvertUtils.convert(projectAttachMapperExt.selectByPrimaryKey(projectAttachId));
    	return dto;
    }

    //新增/更新单个项目附件，传入：ProjectAttachDTO
    public ProjectAttachDTO save(ProjectAttachDTO dto) {
    	ProjectAttach entity;
        if (dto.getAttachId() != null && dto.getAttachId() > 0) {
            entity = projectAttachMapperExt.selectByPrimaryKey(dto.getAttachId());
            entity.setAttachName(dto.getAttachName());
            entity.setAttachTypeCode(dto.getAttachTypeCode());
            entity.setAttachUrl(dto.getAttachUrl());
            entity.setProjectId(dto.getProjectId());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectAttachMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectAttach();
            entity.setAttachName(dto.getAttachName());
            entity.setAttachTypeCode(dto.getAttachTypeCode());
            entity.setAttachUrl(dto.getAttachUrl());
            entity.setProjectId(dto.getProjectId());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectAttachMapperExt.insertSelective(entity);
        }
        dto = ProjectAttachDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目附件
    public Boolean delete(ProjectAttach entity) {
        entity.setIsDelete(true);
        return projectAttachMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }
    
    /**
     * 根据projectId获取附件列表
     * @param projectId
     * @return
     */
    public List<ProjectAttach> getProjectAttachListByProjectId(Integer projectId) {
    	ProjectAttachExample example = new ProjectAttachExample();
    	ProjectAttachExample.Criteria criteria = example.createCriteria();
    	criteria.andProjectIdEqualTo(projectId);
    	criteria.andIsDeleteEqualTo(Boolean.FALSE);
    	List<ProjectAttach> projectAttachList = projectAttachMapperExt.selectByExample(example);

    	return projectAttachList;
    }

    public List<ProjectAttachDTO> saveList(Integer projectId, List<ProjectAttachDTO> saveDtos) {
        List<ProjectAttach> historyList = getProjectAttachListByProjectId(projectId);
        if (historyList != null && historyList.size() > 0) {
            for (ProjectAttach history : historyList) {
                Boolean needDelete = true;
                for (ProjectAttachDTO dto : saveDtos) {
                    if (dto.getAttachId() != null && history.getAttachId() != null && dto.getAttachId().intValue() == history.getAttachId().intValue()) {
                        needDelete = false;
                        break;
                    }
                }
                if (needDelete) {
                    delete(history);
                }
            }
        }
        for (ProjectAttachDTO dto : saveDtos) {
            dto.setProjectId(projectId);
            save(dto);
        }
        return saveDtos;
    }

}
